package cn.com.b2office.struts2.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.b2office.domain.Article;
import cn.com.b2office.domain.Category;
import cn.com.b2office.domain.Item;
import cn.com.b2office.domain.SpecialItem;
import cn.com.b2office.service.ArticleService;
import cn.com.b2office.service.CategoryService;
import cn.com.b2office.service.ItemService;

import com.opensymphony.xwork2.ActionSupport;

public class IndexAction extends ActionSupport {

	private static final long serialVersionUID = -5586175728934642069L;

	private static final Log logger = LogFactory.getLog(IndexAction.class);

	private CategoryService categoryService = null;

	private ArticleService articleService = null;

	private ItemService itemService = null;

	private List<List> list = null;

	private List<Article> articles = null;

	/* 新品推荐 */
	private List<List> newItems = null;

	/* 热卖产品 */
	private List<List> hotItems = null;

	/* 特价产品 */
	private List<List> cheapItems = null;

	public String execute() throws Exception {
		// left category menu
		list = new ArrayList<List>();
		Map<Category, List<Category>> map = categoryService
				.listCascadeCategories();
		Iterator<Category> iterator = map.keySet().iterator();
		while (iterator.hasNext()) {
			Category ct = iterator.next();
			List<Category> childs = map.get(ct);
			List item = new ArrayList();
			item.add(ct);
			item.add(childs);
			list.add(item);
		}

		// article
		articles = articleService.findArticle(2, 0, 7);
		// 新品推荐
		List<SpecialItem> newSpecialItems = itemService.findSpecialItems(1, 0,
				10);
		List<Item> items1 = itemService
				.findItems(getItemIdList(newSpecialItems));
		newItems = buildSpecialItem(newSpecialItems, items1);
		// 热卖商品
		List<SpecialItem> hotSpecialItems = itemService.findSpecialItems(2, 0,
				10);
		List<Item> items2 = itemService
				.findItems(getItemIdList(hotSpecialItems));
		hotItems = buildSpecialItem(hotSpecialItems, items2);
		// 特价商品
		List<SpecialItem> cheapSpecialItems = itemService.findSpecialItems(3,
				0, 10);
		List<Item> items3 = itemService
				.findItems(getItemIdList(cheapSpecialItems));
		cheapItems = buildSpecialItem(cheapSpecialItems, items3);
		return SUCCESS;

	}

	private List<String> getItemIdList(List<SpecialItem> items) {
		List<String> itemIdList = new ArrayList<String>();
		if (items == null || items.isEmpty())
			return itemIdList;
		for (SpecialItem si : items) {
			itemIdList.add(si.getItemId());
		}
		return itemIdList;
	}

	private List<List> buildSpecialItem(List<SpecialItem> specialItems,
			List<Item> items) {
		if (items == null || items.isEmpty())
			return null;
		List<List> results = new ArrayList<List>();
		Map<String, SpecialItem> map = new HashMap<String, SpecialItem>();
		for (SpecialItem si : specialItems) {
			map.put(si.getItemId(), si);
		}
		for (Item item : items) {
			SpecialItem st = map.get(item.getItem_no());
			if (st == null)
				continue;
			List l = new ArrayList();
			l.add(item);
			l.add(st);
			results.add(l);
		}
		return results;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public List<Article> getArticles() {
		return articles;
	}

	public List getList() {
		return list;
	}

	public List<List> getCheapItems() {
		return cheapItems;
	}

	public List<List> getHotItems() {
		return hotItems;
	}

	public List<List> getNewItems() {
		return newItems;
	}
	
	

}
